package com.augurit.smartform.converter;

import com.augurit.common.entity.SysMetaColumn;
import com.augurit.smartform.database.Column;

public class FieldConverter {
	public static Column convertToColumn(SysMetaColumn sysMetaColumn){
		Column column=null;
		if(sysMetaColumn!=null){
			column=new Column();
			column.setTableId(sysMetaColumn.getTableId());
			column.setColumnType(sysMetaColumn.getColumnType());
			column.setPropertyName(sysMetaColumn.getPropertyName());
			column.setFieldId(sysMetaColumn.getFieldId());
			column.setDefaultValue(sysMetaColumn.getDefaultValue());
			column.setDictionary(sysMetaColumn.getDictionary());
			column.setRefTable(sysMetaColumn.getRefTable());
			column.setRefColumn(sysMetaColumn.getRefColumn());
			column.setSqlType(sysMetaColumn.getSqlType());
			
			column.setName(sysMetaColumn.getColumnName());
			column.setCnName(sysMetaColumn.getColumnComments());
			if(sysMetaColumn.getColumnLength()!=null){
				column.setLength(sysMetaColumn.getColumnLength().intValue());
			}
			if(sysMetaColumn.getColumnPrecision()!=null){
				column.setPrecision(sysMetaColumn.getColumnPrecision().intValue());
			}
			if(sysMetaColumn.getColumnScale()!=null){
				column.setScale(sysMetaColumn.getColumnScale().intValue());
			}
			if("Y".equalsIgnoreCase(sysMetaColumn.getNullable())){
				column.setNotNull(Boolean.FALSE);
			}else if("N".equalsIgnoreCase(sysMetaColumn.getNullable())){
				column.setNotNull(Boolean.TRUE);
			}
			if("Y".equalsIgnoreCase(sysMetaColumn.getIsUnique())){
				column.setUnique(Boolean.TRUE);
			}else if("N".equalsIgnoreCase(sysMetaColumn.getIsUnique())){
				column.setUnique(Boolean.FALSE);
			}
			if("Y".equalsIgnoreCase(sysMetaColumn.getIsIndex())){
				column.setIndex(Boolean.TRUE);
			}else if("N".equalsIgnoreCase(sysMetaColumn.getIsIndex())){
				column.setIndex(Boolean.FALSE);
			}
			if("Y".equalsIgnoreCase(sysMetaColumn.getIsPrimaryKey())){
				column.setPrimaryKey(Boolean.TRUE);
			}else if("N".equalsIgnoreCase(sysMetaColumn.getIsPrimaryKey())){
				column.setPrimaryKey(Boolean.FALSE);
			}
			if("Y".equalsIgnoreCase(sysMetaColumn.getIsForeignKey())){
				column.setForeignKey(Boolean.TRUE);
			}else if("N".equalsIgnoreCase(sysMetaColumn.getIsForeignKey())){
				column.setForeignKey(Boolean.FALSE);
			}
		}
		return column;
	}
	public static SysMetaColumn convertToSysMetaColumn(Column column){
		SysMetaColumn sysMetaColumn=null;
		if(column!=null){
			sysMetaColumn=new SysMetaColumn();
			sysMetaColumn.setTableId(column.getTableId());
			sysMetaColumn.setColumnType(column.getColumnType());
			sysMetaColumn.setPropertyName(column.getPropertyName());
			sysMetaColumn.setFieldId(column.getFieldId());
			sysMetaColumn.setDefaultValue(column.getDefaultValue());
			sysMetaColumn.setDictionary(column.getDictionary());
			sysMetaColumn.setRefTable(column.getRefTable());
			sysMetaColumn.setRefColumn(column.getRefColumn());
			sysMetaColumn.setSqlType(column.getSqlType());
			
			sysMetaColumn.setColumnName(column.getName());
			sysMetaColumn.setColumnComments(column.getCnName());
			if(column.getLength()!=null){
				sysMetaColumn.setColumnLength(column.getLength().longValue());
			}
			if(column.getPrecision()!=null){
				sysMetaColumn.setColumnPrecision(column.getPrecision().longValue());
			}
			if(column.getScale()!=null){
				sysMetaColumn.setColumnScale(column.getScale().longValue());
			}
			if(column.isNotNull()){
				sysMetaColumn.setNullable("N");
			}else{
				sysMetaColumn.setNullable("Y");
			}
			if(column.isUnique()){
				sysMetaColumn.setIsUnique("Y");
			}else{
				sysMetaColumn.setIsUnique("N");
			}
			if(column.isIndex()){
				sysMetaColumn.setIsIndex("Y");
			}else{
				sysMetaColumn.setIsIndex("N");
			}
			if(column.isPrimaryKey()){
				sysMetaColumn.setIsPrimaryKey("Y");
			}else{
				sysMetaColumn.setIsPrimaryKey("N");
			}
			if(column.isForeignKey()){
				sysMetaColumn.setIsForeignKey("Y");
			}else{
				sysMetaColumn.setIsForeignKey("N");
			}
		}
		return sysMetaColumn;
	}
	
	public static void convertToSysMetaColumn(Column column,SysMetaColumn sysMetaColumn){
		if(column!=null&&sysMetaColumn!=null){
			if(column.getTableId()!=null){
				sysMetaColumn.setTableId(column.getTableId());
			}
			sysMetaColumn.setColumnType(column.getColumnType());
			sysMetaColumn.setPropertyName(column.getPropertyName());
			sysMetaColumn.setFieldId(column.getFieldId());
			sysMetaColumn.setDefaultValue(column.getDefaultValue());
			sysMetaColumn.setDictionary(column.getDictionary());
			sysMetaColumn.setRefTable(column.getRefTable());
			sysMetaColumn.setRefColumn(column.getRefColumn());
			sysMetaColumn.setSqlType(column.getSqlType());
			
			sysMetaColumn.setColumnName(column.getName());
			sysMetaColumn.setColumnComments(column.getCnName());
			if(column.getLength()!=null){
				sysMetaColumn.setColumnLength(column.getLength().longValue());
			}
			if(column.getPrecision()!=null){
				sysMetaColumn.setColumnPrecision(column.getPrecision().longValue());
			}
			if(column.getScale()!=null){
				sysMetaColumn.setColumnScale(column.getScale().longValue());
			}
			if(column.isNotNull()){
				sysMetaColumn.setNullable("N");
			}else{
				sysMetaColumn.setNullable("Y");
			}
			if(column.isUnique()){
				sysMetaColumn.setIsUnique("Y");
			}else{
				sysMetaColumn.setIsUnique("N");
			}
			if(column.isIndex()){
				sysMetaColumn.setIsIndex("Y");
			}else{
				sysMetaColumn.setIsIndex("N");
			}
			if(column.isPrimaryKey()){
				sysMetaColumn.setIsPrimaryKey("Y");
			}else{
				sysMetaColumn.setIsPrimaryKey("N");
			}
			if(column.isForeignKey()){
				sysMetaColumn.setIsForeignKey("Y");
			}else{
				sysMetaColumn.setIsForeignKey("N");
			}
		}
	}
}
